/** 创建组件对象 * */
var indicatorExtractionForm = new Form();
var indicatorExtractionAjax = new Ajax();
var indicatorExtractionDialog = new Dialog();

var companyObj;
var unitObj;

/** 页面加载完执行 * */
$(function() {
	initScript();
	initConditions();
	initElementsControl();
});

/** 初始化script * */
function initScript() {
	loadScript(appPath + "/project-yddemo/indicatorDataExtraction/locale/IndicatorExtraction_" + appLocale
			+ ".js"); // 1.加载国际化js*
	loadScript(appPath
			+ "/project-yddemo/indicatorDataExtraction/properties/IndicatorExtractionProperties_Add.js"); // 2.加载配置js(非必配,配置也可写在本js中)
}

/** 初始化条件 * */
function initConditions() {
	indicatorExtractionForm.initFormConditions({
		divId : "indicatorExtractionConditionDiv", // 1.归属div*
		conditions : IndicatorExtractionProperties_Add.getAddConditions(), // 2.配置新增页面数据*
		formButtons : [
				{ // 4.操作按钮*
					width : "50%", // 占用宽度
					align : "-10%", // left,center,right,百分比*
					label : "关闭", // 文本*
					clickFun : function() { // 按钮事件*
						window.parent.indicatorExtractionDialog
								.removeDialog("yddemo:indicatorDataExtraction:add");
					}
				}, {
					width : "50%", // 占用宽度
					align : "10%", // left,center,right,百分比*
					label : "确定", // 文本*
					action : "yddemo:indicatorDataExtraction:add",
					clickFun : function() { // 按钮事件*
						saveData();
					}
				} ]
	});
	
	var unitInput = $("[key$='comUnit']");
	unitInput.bind("click", function() {
		if(companyObj==undefined){
			indicatorExtractionDialog.alertDialog(indicatorExtractionLocale.text.selectCompany);
			return false;
		}
		sessionStorage.keyName = "comUnit";
		sessionStorage.selectedId = companyObj.id;
		window.parent.indicatorExtractionDialog.iframeDialog(requestPath
				+ "/yddemo/indicatorDataExtractions/dialog/comUnitDialog",
				indicatorExtractionLocale.field.name, 400, 300,
				"yddemo:indicatorDataExtraction:view", false);
	});
	unitInput.bind("focus",function(){
		unitInput.blur();
	});
	
	var companyInput = $("[key$='company']");
	companyInput.bind("click", function() {
		sessionStorage.keyName = "company";
		window.parent.indicatorExtractionDialog.iframeDialog(requestPath
				+ "/yddemo/indicatorDataExtractions/dialog/companyDialog",
				indicatorExtractionLocale.field.company, 400, 300,
				"yddemo:indicatorDataExtraction:view", false);
	});
	companyInput.bind("focus",function(){
		companyInput.blur();
	});
}

/** 保存数据 * */
function saveData() {
	if (indicatorExtractionForm.validData()) {
		var data = indicatorExtractionForm.getFormConditions();
		if(companyObj!=undefined){
			data.company = companyObj;
		}
		if(unitObj!=undefined){
			data.comUnit = unitObj;
		}
		var resultJson = indicatorExtractionAjax.sendAjax({
			url : requestPath + "/yddemo/indicatorDataExtractions", // 1.请求url*
			params : { // 2.请求url参数(非必配)
				_method : "POST"
			},
			async : false, // 3.是否异步*
			type : "POST", // 4.请求类型*
			data : data
		// 5.请求参数
		});
		if (resultJson.success) {
			window.parent.indicatorExtractionDialog.tipDialog(
					indicatorExtractionLocale.text.addSuccess, function() {
						window.parent.indicatorExtractionTable.reloadData(); // 新增成功后,刷新父对象(刷新indicatorExtraction_View.jsp页面)
						window.parent.disableButton();
						window.parent.indicatorExtractionDialog.closeDialog(); // 新增成功后,关闭父对象弹出框(关闭indicatorExtraction_Add.jsp页面)
					});
		} else {
			window.parent.indicatorExtractionDialog.alertDialog(resultJson.message); // 新增失败后,父对象弹出警告框
		}
	};
}

function closeWindow() {
	window.parent.indicatorExtractionDialog.closeDialog();
}
